package com.kakarote.admin.controller;

import com.kakarote.admin.entity.BO.AdminRouterSaveBO;
import com.kakarote.admin.entity.BO.AdminRouterSaveRequestBO;
import com.kakarote.admin.entity.BO.AdminRouterTitleUpdateBO;
import com.kakarote.admin.service.IAdminRouterService;
import com.kakarote.core.common.Result;
import com.kakarote.core.feign.admin.entity.AdminRouter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 全局路由配置表 前端控制器
 *
 * @author zjj
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/adminRouter")
@Api(tags = "路由配置")
public class AdminRouterController {

    @Autowired
    private IAdminRouterService routerService;

    @PostMapping("/save")
    @ApiOperation("保存路由配置")
    public Result<AdminRouter> save(@RequestBody AdminRouterSaveBO saveBO) {
        AdminRouter adminRouter = routerService.saveRouter(saveBO);
        return Result.ok(adminRouter);
    }

    @PostMapping("/updateTitle")
    @ApiOperation("根据源模块 ID 修改路由标题")
    public Result<String> save(@RequestBody AdminRouterTitleUpdateBO updateBO) {
        routerService.updateTitleByModuleId(updateBO);
        return Result.ok();
    }

    @PostMapping("/batchSave")
    @ApiOperation("批量保存路由配置")
    public Result<String> batchSave(@RequestBody AdminRouterSaveRequestBO requestBO) {
        routerService.batchSaveRouter(requestBO);
        return Result.ok();
    }

    @PostMapping("/saveBatch")
    @ApiOperation("批量保存路由信息")
    public Result<String> saveBatch(@RequestBody List<AdminRouter> routers) {
        routerService.saveBatch(routers);
        return Result.ok();
    }

    @PostMapping("/deleteAll/{applicationId}")
    @ApiOperation("删除应用下的全部路由")
    public Result<String> deleteAll(@PathVariable("applicationId") Long applicationId) {
        routerService.deleteAll(applicationId);
        return Result.ok();
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除路由")
    public Result<String> delete(@PathVariable("id") Long id) {
        routerService.deleteRouter(id);
        return Result.ok();
    }

    @PostMapping("/list/{applicationId}")
    @ApiOperation("查询应用的路由信息")
    public Result<List<AdminRouter>> list(@PathVariable("applicationId") Long applicationId) {
        List<AdminRouter> routers = routerService.listRouter(applicationId);
        return Result.ok(routers);
    }


}
